// 浅拷贝
function shallowCopy(obj) {
  if (typeof obj !== 'object') return
  let copyObj = Array.isArray(obj) ? [] : {}
  for (let key in obj) {
    if (obj.hasOwnProperty(key)) {
      copyObj[key] = obj[key]
    }
  }
  return copyObj
}

function shallowCopy(obj) {
  if (typeof obj !== 'object') return
  let copyObj = Array.isArray(obj) ? [...obj] : { ...obj }
  return copyObj
}

// 深拷贝
function isObject(target) {
  return typeof target === 'object' && target !== null
}
function deepClone(target, map = new WeakMap()) {
  debugger
  if (map.get(target)) {
    return target
  }
  // 获取当前值的构造函数：获取它的类型
  // let constructor = target.constructor
  // // 检测当前对象target是否与正则、日期格式对象匹配
  // if (/^(RegExp|Date)$/i.test(constructor.name)) {
  //   // 创建一个新的特殊对象(正则类/日期类)的实例
  //   return new constructor(target)
  // }
  if (isObject(target)) {
    map.set(target, true)
    const cloneTarget = Array.isArray(target) ? [] : {}
    for (let prop in target) {
      if (target.hasOwnProperty(prop)) {
        cloneTarget[prop] = deepClone(target[prop], map)
      }
    }
    return cloneTarget
  } else {
    return target
  }
}

var obj1 = {
  a: 1,
  b: obj2
};
var obj2 = {}
obj2.a = obj1
var copyObj = deepClone(obj1);
console.log(copyObj);